package com.shopping.service.impl;

import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import com.shopping.commons.response.DataGradResp;
import com.shopping.commons.response.MessageResp;
import com.shopping.commons.util.CommonCode;
import com.shopping.mapper.AdminInfoMapper;
import com.shopping.model.AdminInfo;
import com.shopping.param.AdminInfoParam;
import com.shopping.service.IAdminService;

@Service
public class AdminService implements IAdminService{

	@Autowired
	private AdminInfoMapper adminInfoMapper;
	
	//private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	
	public MessageResp insert(AdminInfo param) {
		MessageResp resp=new MessageResp();
		Random r=new Random();
		param.setAdminid(r.nextInt(10000)+100000);
		if(adminInfoMapper.insertSelective(param)>0){
			resp.setCode(CommonCode.SUCCESS);
			resp.setMsg("添加成功");
			resp.setObj(param);
		}else{
			resp.setCode(CommonCode.FAIL);
			resp.setMsg("添加失败");
		}
		return resp;
	}

	public MessageResp update(AdminInfo param) {
		MessageResp resp=new MessageResp();
		AdminInfo entity=adminInfoMapper.selectByPrimaryKey(param.getAdminid());
		if(entity==null){
			resp.setCode("9999");
			resp.setMsg("未找到修改项");
			return resp;
		}
		entity.setName(param.getName());
		entity.setAccount(param.getAccount());
		entity.setApwd(param.getApwd());
		entity.setDelflag(param.getDelflag()==null?CommonCode.DELFLAG_NORMAL:param.getDelflag());
		entity.setStatus(param.getStatus()==null?CommonCode.STATUS_ONE:param.getStatus());
		
		if(adminInfoMapper.updateByPrimaryKeySelective(entity)>0){
			resp.setCode("0000");
			resp.setMsg("修改成功");
		}else{
			resp.setCode("9999");
			resp.setMsg("修改失败");
		}
		return resp;
	}

	public MessageResp delete(Integer id) {
		MessageResp resp=new MessageResp();
			AdminInfo entity=adminInfoMapper.selectByPrimaryKey(id);
			if(entity==null){
				resp.setCode("9999");
				resp.setMsg("未找到删除项");
				return resp;
			}
			entity.setDelflag(CommonCode.DELFLAG_DELETE);
			if(adminInfoMapper.updateByPrimaryKeySelective(entity)>0){
				resp.setCode("0000");
				resp.setMsg("删除成功");
			}else{
				resp.setCode("9999");
				resp.setMsg("删除失败");
			}
			return resp;
	}

	public DataGradResp<AdminInfo> select(AdminInfo param) {
		DataGradResp<AdminInfo> resp=new DataGradResp<AdminInfo>();
		param.setDelflag(param.getDelflag()==null?"0":param.getDelflag());
		List<AdminInfo> adminInfoList;
		try {
			adminInfoList = adminInfoMapper.select(param);
		} catch (Exception e) {
			e.printStackTrace();
			resp.setCode("9999");
			resp.setMsg("查询成功");
			return resp;
		}
		resp.setCode("0000");
		resp.setMsg("查询失败");
		resp.setRows(adminInfoList);
		resp.setTotal(adminInfoMapper.count(CommonCode.DELFLAG_NORMAL));
		return resp;
	}
	
	/**
	 * 后台管理员登录
	 */
	public MessageResp login(AdminInfoParam param) {
		MessageResp resp=new MessageResp();
		//if(){ return resp}
		if(param.getSearchParam()==null|| param.getSearchParam().equals("")){
			resp.setCode(CommonCode.FAIL);
			resp.setMsg("请完善账号信息");
			return resp;
		}
		if(param.getApwd()==null|| param.getApwd().equals("")){
			resp.setCode(CommonCode.FAIL);
			resp.setMsg("密码不能为空");
			return resp;
		}
		
		param.setDelflag(CommonCode.DELFLAG_NORMAL);
		param.setStatus("1");
		String password=param.getApwd();
		param.setApwd(null);
		AdminInfo retAdmin=null;
		List<AdminInfo> adminInfoList = adminInfoMapper.login(param);
		if(adminInfoList==null|| adminInfoList.isEmpty()){
			resp.setCode(CommonCode.FAIL);
			resp.setMsg("登录失败，没有查到对应用户");
		}else if(!password.equals(adminInfoList.get(0).getApwd())){
			resp.setCode(CommonCode.FAIL);
			resp.setMsg("登录失败，用户名密码不匹配");
		}else{
			retAdmin=adminInfoList.get(0);
			resp.setCode(CommonCode.SUCCESS);
			resp.setMsg("登录成功");
			resp.setObj(retAdmin);
		}
		return resp;
	}

	/**
	 * 实现的这个新的方法  exit  后台用户登出
	 */
	public MessageResp exit(HttpServletRequest request) {
		MessageResp resp=new MessageResp();
		//获取request，然后获取session，然后删除session中 adminInfo这个东西，不操作数据库
		request.getSession().removeAttribute("adminInfo");//这里和之前登录的时候设置进去的保持一致，
		/**
		 * request对象  由Controller直接取
		 * response对象 由controller这一届取
		 * session = request.getSession();
		 * application =request.getSession().getServletContext();就是从session里面取得
		 */
		resp.setCode(CommonCode.SUCCESS);
		request.setAttribute("info-content", "登出成功");//这个内容是放在request里面的，下次在请求就清空了，所以不用担心
		resp.setMsg("登出成功");
		return resp;
	}

	public MessageResp batchDelete(String ids) {
		// TODO Auto-generated method stub
		return null;
	}

}
